﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NamTrungBS.DataLayer
{
    class ChiTietHoaDonDao
    {
        private static NamTrungDBDataContext db = new NamTrungDBDataContext();
        public static List<ChiTietHoaDon> GetChiTietHd()
        {
           
            return db.ChiTietHoaDons.ToList();

        }
        public static List<ChiTietHoaDon> GetChiTietHdByID(int mahoadon)
        {
            return db.ChiTietHoaDons.Where(o=>o.MaHD == mahoadon).ToList();
        }
        public static bool NewHoaDonChiTiet(ChiTietHoaDon chitiet)
        {
            try
            {
               
                    db.ChiTietHoaDons.InsertOnSubmit(chitiet);
                    db.SubmitChanges();
                
                return true;
            }
            catch(Exception ex){
                throw ex;
            }
        }
        /// <summary>
        /// Lay 1 chitiethoadon boi id
        /// </summary>
        /// <param name="maChitiet"></param>
        /// <returns></returns>
        public static ChiTietHoaDon ChiTietHoaDonItem(int maChitiet)
        {
            ChiTietHoaDon item = db.ChiTietHoaDons.Single(ct => ct.MaChiTiet == maChitiet);
            return item;
        }
        public static bool UpdateHoaDonChiTiet(ChiTietHoaDon item)
        {
            try
            {

                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Tính tổng tiền
        /// </summary>
        /// <param name="mahoadon"></param>
        /// <returns></returns>
        public static Double GetSumMoney(int mahoadon)
        {
            var list = from ct in db.ChiTietHoaDons.Where(s => s.MaHD == mahoadon)
                      select ct;
            Double tongtien = 0;
            foreach (var item in list)
            {
                tongtien += Double.Parse(item.ThanhTien.ToString());
            }
            return tongtien;
        }
    }
}
